/************************************************
*@brief:		This class manipulates Strings
*@author:		K. TANGAO
*@file:			formatingClass.cpp
*@date:			27/11/2008
*@Version:		1.0
*************************************************/

#include <windows.h>
//include OpenGL stuff needed to draw a text
#include <gl/gl.h>
#include <gl/glu.h>
#include <GL/glut.h>

#include <stdio.h>

#include "formatingClass.h"
#include "RssBrowsing.h"


using namespace std;
using namespace glfont;

formatString::formatString(string mystring): myFont(new GLFont())
{
	//loadFont(fontLocation);
	//setInterLine(-1);
	setString(mystring);
	wordTable = new string[nbWords];
	setWordTable();
}

formatString::~formatString()
{
	if (myFont != NULL)myFont->Destroy();
	if (wordTable != NULL) delete wordTable;
}

long formatString::getNbChar()
{
	return nbChar;
}


long formatString::getNbWords()
{
	return nbWords;
}

string * formatString::getWordTable()
{
	return wordTable;
}


void formatString::setNbChar()
{
	nbChar = (long)theString.length();
}

void formatString::setNbWords()
{	
	if(theString.length() != 0)
	{
        string strTemp(theString);
        string ponct(",;.:!?");
        unsigned int i,j;
        long number = 0;
        for(i=0; i < strTemp.length(); i++)
		{
            for(j=0; j< ponct.length(); j++)
			{
                if(ponct.find_first_of(strTemp[i]) != -1) strTemp[i]=' ';
            }
        }

        //Count the number of WORDS
        for(i=0; i< strTemp.length()-1; i++)
		{
            if((strTemp[i] == ' ') && (strTemp[i+1] != ' ')) number++;
        }
        nbWords = number+1;
    } 
	else 
	{
        nbWords = 0;
    }
}


void formatString::setWordTable()
{	
	int count=0;
	int i=0, j=0;

	for(i =0; i<nbWords; i++)
	{	
		j=count;
		for(j; j<theString.length(); j++)
		{
			if(theString[j]!= ' ')
			{	
				count++;
				wordTable[i].append(1, theString[j]);
			}
			else
			{
				count++;
				break;
			}
		}
	}
}


GLFont* formatString::getFontUse()
{
	return myFont;
}


bool formatString::loadFont(string fontLocation, unsigned int tex)
{
	//GLuint tex;
	//glGenTextures(1, &tex);
	return myFont->Create(fontLocation, tex);
}


void formatString::setString(std::string newString)
{
	theString = newString;
	setNbChar();
	setNbWords();
	//setWordTable();
}

void formatString::initialize(std::string fontLocation, unsigned int tex)
{
	loadFont(fontLocation, tex);
	setInterLine(-1);
}

int formatString::getWordWidthP(string word)
{
	std::pair<int, int> values = std::make_pair(0,0);
	myFont->GetStringSize(word, &values);
	return (values.first + 1);
}


int formatString::getInterLine()
{
	return interLine;
}

void formatString::setInterLine(int interLineValue)
{	
	if(interLineValue == -1) interLine = (int)(0.25*(getLineHeightP()));
	else interLine = interLineValue;
}


int formatString::getLineHeightP()
{
	std::pair<int, int> values = std::make_pair(0,0);
	myFont->GetStringSize("GLfont", &values);
	return values.second;
}


void formatString::drawWord(float wordXPos, float wordYPos, string word)
{	
	myFont->DrawString(word, 1.0F, wordXPos, wordYPos);	
}


void formatString::drawLine(int widthP, float xStart, float yStart, int* nextWordToDraw, int startPos)
{
	int reste = widthP;
	float wordXPos = xStart;
	int wordWidth;
	int spaceP = getWordWidthP(" ");
	for(int i=startPos; i<nbWords; i++)
	{	
		wordWidth = getWordWidthP(wordTable[i]);
		if(reste > wordWidth)
		{
			drawWord(wordXPos, yStart, wordTable[i]);
			wordXPos += (wordWidth + spaceP);
			reste = (int)(widthP - (wordXPos - xStart));
			*nextWordToDraw = i+1;
		}
		else 
		{
			*nextWordToDraw = i;
			break;
		}
	}
}


void formatString::drawTheWholeText(int widthP, int HeightP, float PosX, float PosY)
{
	int heightLeft = HeightP;
	int heightLine = getLineHeightP();
	//printf("heightLeft= %i, heightLine =%i \n", heightLeft, heightLine);

	int pos=0;
	int i=0;


	while(pos < nbWords)
	{
		if(heightLeft > heightLine)
		{
			drawLine(widthP, PosX, (PosY - i*(interLine + heightLine)), &pos, pos);
			//drawLine(widthP, PosX, (PosY - i*(100 + heightLine)), &pos, pos);
			heightLeft -= (interLine + heightLine);
			i++;
		}
		else break;
	}
}
